<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\UserService;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends BaseController
{
    //用户管理

    /**
     * 微信授权获取的信息
     * avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
        city: ""
        country: ""
        gender: 0
        is_demote: true
        language: ""
        nickName: "微信用户"
        province: ""
     */

    public function __construct(UserService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = (new UserService())->getOption();
        return $this->toJson(0, '', $res);
    }

    //用户列表
    public function getUserIndex(Request $request)
    {
        $pas['phone'] = $request->post('phone', '');
        $pas['nick'] = $request->post('nick', '');
        $pas['level'] = $request->post('level', '');
        $pas['level'] = $request->post('level', '');
        $pas['id'] = $request->post('id', 0);
        $pas['serial'] = $request->post('serial', '');
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new UserService())->getUserList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    public function del(Request $request){
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = (new UserService())->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
    //浏览历史
    public function getTraceIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $uid = $request->post('uid', 0);
        $res = (new UserService())->getTraceList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //收货地址
    public function getAddressIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $uid = $request->post('uid', 0);
        $res = (new UserService())->getAddressList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //设置默认地址
    public function setAddressDefault(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => 'ID必须的',
//        ]);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = (new UserService())->setAddressDefault($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //协议获取
    public function xieyiOne(Request $request)
    {
        $txt = file_get_contents('xieyi.json');
        if (!$txt) {
            return $this->toJson(-2, '没有内容');
        }
        $txt = json_decode($txt, true);
        return $this->toJson(0, '', $txt);
    }

    //协议保存
    public function setXieyi(Request $request)
    {
        $xieyi = $request->post('xieyi', '');
        if (!$xieyi) {
            return $this->toJson(-2, '没有设置内容');
        }
        file_put_contents('xieyi.json', json_encode($xieyi, 384));
        return $this->toJson();
    }

    //权益获取
    public function qyOne(Request $request)
    {
        $txt = file_get_contents('quanyi.json');
        if (!$txt) {
            return $this->toJson(-2, '没有内容');
        }
        $txt = json_decode($txt, true);
        return $this->toJson(0, '', $txt);
    }

    //权益保存
    public function setQy(Request $request)
    {
        $xieyi = $request->post('quanyi', '');
        if (!$xieyi) {
            return $this->toJson(-2, '没有设置内容');
        }
        file_put_contents('quanyi.json', json_encode($xieyi, 384));
        return $this->toJson();
    }

    //微信配置获取
    public function wxConfigOne(Request $request)
    {
        $txt = file_get_contents('weixin.json');
        if (!$txt) {
            return $this->toJson(-2, '没有配置');
        }
        $txt = json_decode($txt, true);
        return $this->toJson(0, '', $txt);
    }

    //微信配置设置
    public function wxConfigModify(Request $request)
    {
        $pas = [
            'xcx_pay_appid' => $request->post('appid', ''),
            'xcx_pay_mchid' => $request->post('mchid', ''),
            'notify_url' => $request->post('notify_url', ''),
            'apiclient_key' => $request->post('apiclient_key', ''),
        ];
        if (!$pas['xcx_pay_appid']) {
            return $this->toJson(-2, 'appid必须的');
        }
        if (!$pas['xcx_pay_mchid']) {
            return $this->toJson(-2, 'mchid必须的');
        }
        $txt = json_encode($pas, 384);
        file_put_contents('weixin.json', $txt);
        return $this->toJson();
    }

    //用户充值记录
    public function getRechargeIndex(Request $request)
    {
        $uid = $request->post('uid', 0);
        $type = $request->post('type', 1);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        if ($type == 1) {
            $res = (new UserService())->getRechargeIndex($uid, $pages);
        } else {
            $res = (new UserService())->getCardIndex($uid, $pages);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //用户提现记录
    public function getTixianIndex(Request $request)
    {
        $uid = $request->post('uid', 0);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new UserService())->getTixianIndex($uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //用户消费统计
    public function getOrderIndex(Request $request)
    {
        $uid = $request->post('uid', 0);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new UserService())->getOrderIndex($uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

//    //设置VIP折扣率
//    public function setVip(Request $request)
//    {
//        $id = $request->post('id', 0);
//        $ln = $request->post('ln', '');
//        $discount = $request->post('discount', 0);
//        $res = $this->db->setVip($id, $ln, $discount);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }

    //设置VIP折扣率
    public function setVip(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['is_auto'] = $request->post('type', 0);
        $pas['discount'] = $request->post('discount', 0);
        $pas['level_id'] = $request->post('levelid', 0);
        if ($pas['is_auto'] == 2 && !$pas['level_id']) {
            return $this->toJson(-2, '会员等级必须的');
        }
        if ($pas['is_auto'] == 3 && !$pas['discount']) {
            return $this->toJson(-2, '折扣率必须的');
        }
        $res = $this->db->setVip($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //卡号列表
    public function bankList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $uid = $request->post('uid', 0);
        $res = $this->db->bankList($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //卡号默认
    public function bankDefaultModify(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->bankDefaultModify($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //提现审核列表
    public function txAuthList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->txAuthList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //提现审核修改
    public function txAuthModify(Request $request)
    {
        $id = $request->post('id', 0);
        $auth = $request->post('auth', 0); //1拒绝，2通过
        if (!in_array($auth, [1, 2])) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->txAuthModify($id, $auth);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 分销列表
     */
    public function fxlist(Request $request)
    {
        $uid = $request->post('uid', 0);
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->fxlist($pages, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //测试导出
    public function test()
    {
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('ttt',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xlsx');
//        try {
//            Excel::create('ttt',function($excel) use ($cellData){
//                var_dump($cellData);
//                $excel->sheet('score', function($sheet) use ($cellData){
//                    $sheet->rows($cellData);
//                });
//            })->export('xlsx');
//        } catch (\Exception $e){
//            echo $e->getMessage();
//        }
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['nickname'] = $request->post('nickname', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['avatarUrl'] = $request->post('img', '');
        $res = $this->db->modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //导出功能
    public function getOut(Request $request)
    {
        $pas['phone'] = $request->post('phone', '');
        $pas['nick'] = $request->post('nick', '');
        $res = (new UserService())->getUlist($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $titles = ['ID', '昵称', '手机号', '头像地址', '钱包余额', '当前等级', '浏览量', '收货地址', '优惠劵数量', '充值量', '时间'];
        $fname = 'daochu/yonghu-'.time().'.txt';
        file_put_contents($fname, implode('<-->', $titles).PHP_EOL, FILE_APPEND);
        foreach ($res as $v) {
            $arys = [
                $v['id'], $v['nickname'], $v['phone'] ,$v['balance'], $v['levelName'],
                $v['readNum'], $v['addressNum'], $v['couponNum'] ,$v['rechargeNum'], $v['lasttime'],
            ];
            file_put_contents($fname, implode('<-->', $arys).PHP_EOL, FILE_APPEND);
        }
        $fpath = env('DOMAIN').'/'.$fname;
        return $this->toJson(0, '', $fpath);
    }
}